HAL COMP initialization and configuration functions migration ¶
The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:
Open STM32CubeMX2 COMP configuration panel.
Configure the SW layer HAL.
Configure the COMP main features.
Configure the additional COMP features if needed.
Configure the COMP GPIO pins.
Configure the COMP EXTI if the application requires it.
Configure the NVIC if the application requires the COMP to be used in IT mode.
Generate the code
Note
The configuration below might varies from a series to another.
HAL COMP configuration panel ¶
Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.
HAL COMP main features configuration ¶
Configure the COMP window mode
Configure the COMP power mode
Configure the COMP input
Configure the COMP output
HAL COMP additional features configuration ¶
Configure the additional COMP features if needed.
GPIO configuration ¶
Configure gpio pins.
EXTI configuration ¶
Configure the EXTI as needed.
NVIC configuration ¶
Configure the NVIC in case of an interrupt.
Generated COMP initialization and de-initialization sequences ¶
The COMP initialization sequence is generated in the
mx_comp1_init
function in
mx_comp1.c file
under
$YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.
Related concepts:
|
Topic |
Code Snippet HAL1 |
Code Snippet HAL2 |
|---|---|---|
|
Handle declaration |
COMP_HandleTypeDef hcomp1;
|
static hal_comp_handle_t hCOMP1;
|
|
Init, config and Msp sequence function |
void MX_COMP1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Enable COMP12 clock */
__HAL_RCC_COMP12_CLK_ENABLE();
/* Configure COMP1 */
hcomp1.Instance = COMP1;
hcomp1.Init.InvertingInput = COMP_INPUT_MINUS_1_4VREFINT;
hcomp1.Init.NonInvertingInput = COMP_INPUT_PLUS_DAC1_CH1;
hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
hcomp1.Init.Mode = COMP_POWERMODE_HIGHSPEED;
hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE;
hcomp1.Init.BlankingSrce = COMP_BLANKINGSRC_TIM1_OC5;
hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING;
if (HAL_COMP_Init(&hcomp1) != HAL_OK)
{
Error_Handler();
}
/* Enable GPIOB clock */
__HAL_RCC_GPIOB_CLK_ENABLE();
/* COMP1 GPIO Configuration: PB1 --> COMP1_OUT */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF14_COMP1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Configure NVIC for COMP1 interrupt */
HAL_NVIC_SetPriority(COMP1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(COMP1_IRQn);
}
|
hal_comp_handle_t *mx_comp1_init(void)
{
HAL_RCC_COMP12_EnableClock();
if (HAL_COMP_Init(&hCOMP1, HAL_COMP1) != HAL_OK)
{
return NULL;
}
hal_comp_config_t comp_config;
comp_config.power_mode = HAL_COMP_POWER_MODE_HIGH_SPEED;
comp_config.input_plus = HAL_COMP_INPUT_PLUS_DAC1_CH1;
comp_config.input_minus = HAL_COMP_INPUT_MINUS_1_4VREFINT;
comp_config.input_hysteresis = HAL_COMP_INPUT_HYSTERESIS_NONE;
comp_config.output_polarity = HAL_COMP_OUTPUT_POLARITY_NONINVERTED;
#if defined(USE_HAL_COMP_EXTI) && (USE_HAL_COMP_EXTI == 1)
comp_config.output_trigger = HAL_COMP_OUTPUT_TRIG_RISING;
#endif /* USE_HAL_COMP_EXTI */
if (HAL_COMP_SetConfig(&hCOMP1, &comp_config) != HAL_OK)
{
return NULL;
}
if (HAL_COMP_SetOutputBlanking(&hCOMP1, HAL_COMP_OUTPUT_BLANK_TIM1_OC5) != HAL_OK)
{
return NULL;
}
HAL_RCC_GPIOB_EnableClock();
hal_gpio_config_t gpio_config;
/**
COMP1 GPIO Configuration
[GPIO Pin] ------> [Signal Name]
PB1 ------> COMP1_OUT
**/
gpio_config.mode = HAL_GPIO_MODE_ALTERNATE;
gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
gpio_config.pull = HAL_GPIO_PULL_NO;
gpio_config.speed = HAL_GPIO_SPEED_FREQ_LOW;
gpio_config.alternate = HAL_GPIO_AF_14;
HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_1, &gpio_config);
/* Enable the interruption for COMP */
HAL_CORTEX_NVIC_SetPriority(COMP1_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(COMP1_IRQn);
return &hCOMP1;
}
|
|
Deinit sequence function |
void MX_COMP1_DeInit(void)
{
HAL_COMP_DeInit(&hcomp1);
}
|
void mx_comp1_deinit(void)
{
(void)HAL_COMP_DeInit(&hCOMP1);
HAL_RCC_COMP12_DisableClock();
HAL_RCC_COMP12_Reset();
/* Disable the interruption for COMP */
HAL_CORTEX_NVIC_DisableIRQ(COMP1_IRQn);
/* De-initialize all GPIO pins associated with COMP1 */
HAL_GPIO_DeInit(HAL_GPIOB, HAL_GPIO_PIN_1);
}
|